package praktikumid.w07.p3;

import java.awt.BorderLayout;
import java.awt.Color;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class FrameExample extends JFrame {

	public static void main(String[] args) {
		
		/*
		 * Could also be just
		 * JFrame frame = new JFrame();
		 * instead, we extend JFrame
		 * This allows some extended functionality
		 * later on.
		 * For this week, new JFrame() would be sufficient.
		 */
		JFrame frame = new FrameExample();
		frame.setTitle("My app");
		
		/*
		 * By default, if we close the window
		 * from "X", the application itself
		 * keeps on running. We have to define
		 * that the close operation would
		 * exit the program.
		 */
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// location, size in pixels
		frame.setLocation(100, 100);
		frame.setSize(300, 200);
		
		/*
		 * Panels are used as wrappers
		 * for other components like buttons,
		 * labels etc.
		 * In this example, we want to divide
		 * our window into 2 sections:
		 * left side
		 * and right side (main panel)
		 * 
		 * 
		 */
		// left
		JPanel leftPanel = new JPanel();
		/*
		 * Layout manager is used to place components inside
		 * the panel. BoxLayout allows to put components
		 * in one column. The default layout is "float",
		 * which puts components right next (right) to the
		 * previous element.
		 * Here we want to put one button below another button.
		 */
		leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
		/*
		 * Let's change background color to see different
		 * panels.
		 * Setting color can be done via new Color(r, g, b)
		 * or using predefined color constants Color.RED.
		 */
		leftPanel.setBackground(new Color(100, 100, 100));
		leftPanel.setBackground(Color.GREEN);
		leftPanel.setSize(100, 300);
		
		/*
		 * Frame consists of content pane and menu bar.
		 * See
		 * http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html
		 * for pictures
		 * Everything should be placed on the content pane
		 * (not on the frame itself). Although technically
		 * frame.add automatically puts component on the content
		 * pane. But to emphasize that we are putting
		 * components on the content pane, it's
		 * frame.getContentPane().*
		 * 
		 * Every component (except for the JFrame)
		 * should have a container component. Therefore,
		 * every component should be added to some other
		 * component. It's done like that:
		 * container.add(component)
		 * Depending on the layout manager, you can
		 * define the location:
		 * container.add(component, location)
		 * BorderLayout allows to put components
		 * on different sides (EAST, WEST, etc).
		 */
		frame.getContentPane().setLayout(new BorderLayout());
		frame.getContentPane().add(leftPanel, BorderLayout.WEST);
		
		/*
		 * Some buttons, where the argument for 
		 * the constructor is the title of the button.
		 */
		JButton button1 = new JButton("Press me");
		leftPanel.add(button1);
		JButton button2 = new JButton("Press me too");
		leftPanel.add(button2);
		
		// right
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		mainPanel.setBackground(Color.WHITE);
		
		/*
		 * JLabel is used to position some text in to the panel.
		 */
		JLabel label1 = new JLabel("some text");
		mainPanel.add(label1);
		JLabel label2 = new JLabel("some other text");
		mainPanel.add(label2);
		
		/*
		 * CENTER automatically tries to fill all the available space.
		 * You can try EAST here, which leaves the center empty.
		 */
		frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		
		/*
		 * pack() tries to find the optimal size for the window.
		 * Usually it's not very nice, setting size yourself
		 * can yield in better results.
		 */
		frame.pack();
		/*
		 * By default, new frame will not be visible
		 * (it's created in background).
		 * To make it visible, we have to set visible
		 * to true.
		 */
		frame.setVisible(true);
		/*
		 * Components have paint()
		 * method, which should not be called directly.
		 * Instead, repaint() is used, which calls paint()
		 * itself, but does it "nicely".
		 * 
		 * Here, there's no point to call repaint().
		 * Later on with other examples we will
		 * talk more about painting/repainting.
		 * 
		 * see more:
		 * http://www.oracle.com/technetwork/java/painting-140037.html
		 * and:
		 * http://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html
		 */
		frame.repaint();
	}

}
